export default {
	data() {
		return {
			id: '',
			detail: {},
			proList: [],
			loaded: ''
		}
	},
	async onLoad(options) {
		console.log("orderDetail index onLoad: ", options);
		this.id = options.id;
		await this.getDetail();
	},
	async onShow() {
		const self = this;
		setTimeout(() => {
			self.loaded = 'loadedStatus';
		}, 500)
	},
	onHide() {},
	methods: {
		async getDetail() {
			const detailRes = await this.$uniCloudCallFunction({
				name: 'xxf-order',
				action: "getOrderDetail",
				data: {
					uid: this.$store.state.login.uid,
					id: this.id
				}
			});
			console.log("detailRes: ", detailRes);
			this.detail = detailRes;
			this.proList = detailRes.product_list;
			uni.setNavigationBarTitle({
				title: ["", "待付款", "待发货", "已发货", "已收货", "已失效", "已退款", "已取消"][detailRes.status]
			})
		},
		async cancelOrder(e) {
			if (await this.$utils.showModal(this, {
					title: '取消订单',
					content: `您确认取消该订单？\n(No：${e._id})\n若需退款将原路退回。`,
				})) {
				const cancelRes = await this.$uniCloudCallFunction({
					name: 'xxf-order',
					action: "cancelOrder",
					data: {
						uid: this.$store.state.login.uid,
						orderId: e._id
					}
				});
				console.log("cancelRes: ", cancelRes);
				if (cancelRes) {
					await this.$utils.showToast(this, {
						message: '订单已取消！',
						mode: 1
					});
					await this.getDetail();
				}
			}
		},
		async payOrder(e) {
			const payRes = await this.$uniCloudCallFunction({
				name: 'xxf-order',
				action: "payOrder",
				data: {
					uid: this.$store.state.login.uid,
					openId: this.$store.state.login.openId,
					orderId: e._id
				}
			});
			console.log("payRes: ", payRes);
			const self = this;
			if (payRes) {
				uni.requestPayment({
					timeStamp: payRes.timeStamp,
					nonceStr: payRes.nonceStr,
					package: payRes.package, //统一下单接口返回的 prepay_id 格式如：prepay_id=***
					signType: payRes.signType || 'MD5',
					paySign: payRes.paySign, //签名
					success(res) {
						console.log("支付成功", res)
						self.$utils.showToast(self, {
							message: '支付成功！',
							mode: 1
						});
					},
					fail(res) {
						console.log("支付失败", res)
						self.$utils.showToast(self, {
							message: '支付失败！',
							mode: 2
						});
					},
					complete(res) {
						console.log("支付完成", res);
						try {
							self.$uniCloudCallFunction({
								name: 'xxf-order',
								action: "checkOrderStatus",
								data: {
									uid: self.$store.state.login.uid,
									openId: self.$store.state.login.openId,
									orderId: e._id
								}
							});
						} catch (e) {
							console.log("complete checkOrderStatus", e);
						}
						setTimeout(() => {
							this.getDetail();
						}, 2500);
					}
				})
			}
		},
		async getDelivery(e) {
			if (e.waybill_num) {
				let detailList = [];
				for (let i = 0; i < e.product_list.length; i++) {
					detailList.push({
						goods_name: e.product_list[i].productInfo.name,
						goods_img_url: e.product_list[i].productInfo.thumb,
					})
				}
				const getRes = await this.$uniCloudCallFunction({
					name: 'xxf-order',
					action: "getWaybillToken",
					data: {
						uid: this.$store.state.login.uid,
						open_id: this.$store.state.login.openId,
						order_id: e._id,
						receiver_phone: e.receive_info.phone,
						waybill_num: e.waybill_num,
						goods_info: {
							detail_list: detailList
						},
						trans_id: e.notify_info.transactionId,
					}
				});
				console.log("getRes: ", getRes);
				const waybillToken = getRes.waybill_token || "";
				if (waybillToken) {
					//必须用预览才能测试这个功能，无法在工具端模拟
					plugin.openWaybillTracking({
						waybillToken: waybillToken
					});
				} else {
					this.$utils.showToast(this, {
						message: '查询失败',
						mode: 2
					});
				}
			}
		},
		async onReceived(e) {
			if (await this.$utils.showModal(this, {
					title: '收货',
					content: `请您务必确认已经收到货了？\n(No：${e._id})`,
					confirmTxt: '已收货',
				})) {
				console.log("已经收到货了！");
				const receivedRes = await this.$uniCloudCallFunction({
					name: 'xxf-order',
					action: "receivedOrder",
					data: {
						order_id: e._id,
					}
				});
				console.log("receivedRes: ", receivedRes);
				if (receivedRes) {
					this.$utils.showToast(this, {
						message: '收货成功！',
						mode: 2
					});
					await this.getDetail();
				}
			}
		},
		async toService(e) {
			if (await this.$utils.showModal(this, {
					mode: 1,
					title: '售后',
					content: `您可以联系客服为您处理？\n(No：${e._id})`,
					cancelTxt: '不需要了',
					confirmTxt: '复制并联系客服',
				})) {
				console.log("复制单号并联系！");
			}
		},

	},
}